package oa.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Department;
import oa.entity.User;
import oa.paging.Page;
import oa.service.UserService;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class DepartShowList
 */
@WebServlet("/departshowlist")
public class DepartShowList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DepartShowList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		DepartmentServiceImpl depart = new  DepartmentServiceImpl();
		UserService userService = new UserServiceImpl();
		HttpSession session = request.getSession();
		Page<Department> pageInfo = new Page<Department>();
		 List<Department> list = new ArrayList<Department>();
		 int counnts=0;
		 String pageNo = request.getParameter("pageNo");			
		 if(pageNo==null){
				pageNo="1";
			}
		
		 try {
		
			int companyid=(int)session.getAttribute("companyid");
		    String companyids= request.getParameter("companyid");
		    pageInfo.setPageSize(6);
		    pageInfo.setCurrentPage(Integer.parseInt(pageNo));
		    if(companyid==0&&companyids==null){
		    
		    	list = depart.selectAllDepartment(companyid,Integer.parseInt(pageNo), 10);
		   	 counnts=depart.countDepartment(companyid);
		    	
		    } else if(companyid==0&&companyids!=null){
		    
		    	list = depart.selectAllDepartment(Integer.parseInt(companyids),Integer.parseInt(pageNo), 10);
		   	 counnts=depart.countDepartment(Integer.parseInt(companyids));
		    
		    }
		    
		    
		    else if(companyids!=null){
		    	list = depart.selectAllDepartment(Integer.parseInt(companyids),Integer.parseInt(pageNo), 10);
		    	 counnts=depart.countDepartment(Integer.parseInt(companyids));
		    	
		    }else if(companyid!=0&&companyids==null) {
		      	 counnts=depart.countDepartment(companyid);
		    	list = depart.selectAllDepartment(companyid,Integer.parseInt(pageNo), 10);
		    	
		    }
		    pageInfo.setPageList(list);
		    pageInfo.setTotalRow(counnts);
			List<User> userList = userService.selectAllUser(null, 0, 0);		
			request.getSession().setAttribute("userList",userList);
			request.getSession().setAttribute("pageInfolist",pageInfo);
		
			request.getRequestDispatcher("dept_list.jsp").forward(request, response);
		} catch (Exception e) {
			
		}
	   
	}

	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
